<%@ tag body-content="empty" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="spring-sec" uri="http://www.springframework.org/security/tags" %>
<%@ taglib prefix="cf" uri="http://kyub.com/jsp/jstl/functions" %>
<%@ attribute name="pojoDescriptor" required="true" type="com.kyub.spring.gears.model.PojoDescriptor" %>
<%@ attribute name="pojo" required="true" type="java.lang.Object" %>
<%@ attribute name="updateObject" required="false" type="java.lang.Object" %>
<%@ attribute name="relatedValues" required="false" type="java.util.Map" %>
<%@ attribute name="gears_restfull_prefix" required="true" %>
<%@ attribute name="commandPojoName" required="false"  %>
<%@ attribute name="actionName" required="false"  %>

<c:if test="${empty commandPojoName}">
    <c:set var="commandPojoName"  value="commandPojo" />
</c:if>
<c:if test="${empty actionName}">
    <c:set var="actionName"  value="${pojoDescriptor.name}/submit" />
</c:if>
<script type="text/javascript">
    //NOT MY FAULT JQUERY UI BUG
    $(".date-pick").datepicker();
    $("#ui-datepicker-div").addClass("promoteZ");
</script>
<style type="text/css">
    .promoteZ {
        z-index: 2009; /* Dialog z-index is 1006*/
} 
</style>




    <form:form commandName="${commandPojoName}" method="POST" id="pojoForm"
               action="${pageContext.request.contextPath}/${gears_restfull_prefix}/${actionName}">
        <table>

            <c:forEach items="${pojoDescriptor.formFields}" var="p">
                <tr>
                    <td>
                        <spring:message code="${pojoDescriptor.name}.${p.name}" text="${p.name}" htmlEscape="true"/>
                        :
                    </td>
                    <td>
                        <spring:bind path="commandPojo.${p.name}">
                            <c:if test="${cf:isSimpleProperty(p)}">
                                <c:choose>
                                    <c:when test="${p.typeClass.simpleName == 'Date'}">
                                        <form:input path="${p.name}" cssClass="date-pick"/>
                                    </c:when>
                                    <c:when test="${p.typeClass.simpleName == 'Boolean'}">
                                        <form:checkbox path="${p.name}"/>
                                    </c:when>
                                    <c:otherwise>
                                        <form:input path="${p.name}"/>
                                    </c:otherwise>
                                </c:choose>


                            </c:if>
                            <c:if test="${cf:isRelation(p)}">
                                <form:select path="${p.name}">
                                    <form:options items="${relatedValues[p.name]}" itemLabel="name"/>
                                </form:select>
                            </c:if>
                            <c:if test="${cf:isFile(p)}">
                                 <form:hidden path="${p.name}"/>
                                <c:choose>
                                 <c:when test="${updateObject}">
                                    <a href="${pageContext.request.contextPath}/${gears_restfull_prefix}/${pojoDescriptor.name}/download/${cf:readProperty(commandPojo,pojoDescriptor.idProperty)}/${p.name}" >
                                    <spring:message code="${pojoDescriptor.name}.labelDownload" text="Download"
                                                    htmlEscape="true"/>

                                </a>
                                 </c:when>
                                    <c:otherwise>
                                         <spring:message code="noneLabel" text="None"
                                                    htmlEscape="true"/>
                                    </c:otherwise>
                                </c:choose>
                            </c:if>

                            <c:if test="${cf:isCustomWidget(p)}">
                                
                                 <jsp:include flush="true" page="${p.layout.widgetUri}" >
                                      <jsp:param name="pname" value="${p.name}"  />
                                      <jsp:param name="pvalue" value="${cf:readProperty(commandPojo, p.name)}"  />
                                 </jsp:include>
                            </c:if>

                            <c:if test='${status.error}'>
            <span class="errorExplanation">
                    ${status.errorMessage}
            </span>
                            </c:if>
                        </spring:bind>
                    </td>
                </tr>
            </c:forEach>

            <tr>
                <td colspan="2">

                </td>
            </tr>
        </table>
        <c:if test="${updateObject}">
            <form:hidden path="${pojoDescriptor.idProperty}"/>
        </c:if>
        <c:if test="${not empty pageContext.request.parameterMap.backPojo[0]}">
            <input type="hidden" value="${pageContext.request.parameterMap.backPojo[0]}" name="backPojo"
                   id="backPojo"/>
        </c:if>
        <input type="hidden" name="is_ajax" value="true" /> 


    </form:form>

   <h3>

        <spring:message code="attachmentsLabel" text="Attachmes"
                                                    htmlEscape="true"/>
   </h3>



     <c:forEach items="${pojoDescriptor.formFields}" var="p">
          <c:if test="${cf:isFile(p)}">
          <script type="text/javascript">
           $(document).ready(function() {
var options = {
    target:        '#output_upload_${p.name}',   // target element(s) to be updated with server response
    beforeSubmit:  startProgress,  // pre-submit callback
    success:       setUploadedPathValue  // post-submit callback

    // other available options:
    //url:       url         // override for form's 'action' attribute
    //type:      type        // 'get' or 'post', override for form's 'method' attribute
    //dataType:  null        // 'xml', 'script', or 'json' (expected server response type)
    //clearForm: true        // clear all form fields after successful submit
    //resetForm: true        // reset the form after successful submit

    // $.ajax options can be used here too, for example:
    //timeout:   3000
};

// bind to the form's submit event
$('#uploadForm_${p.name}').submit(function() {
    // inside event callbacks 'this' is the DOM element so we first
    // wrap it in a jQuery object and then invoke ajaxSubmit

    uploadPropertyName = '${p.name}'
    $(this).ajaxSubmit(options);

    // !!! Important !!!
    // always return false to prevent standard browser submit and page navigation
    return false;
});
});

          </script>
         <div id="uploadContainer_${p.name}">
             <c:set var="pojoid4upload" value="${cf:readProperty(pojo,pojoDescriptor.idProperty)}" />

             <c:if test="${pojoid4upload == null}">
                    <c:set var="pojoid4upload" value="NEW_OBJ" />
            </c:if>
             <form:form commandName="commandUpload" method="POST" id="uploadForm_${p.name}"
              action="${pageContext.request.contextPath}/${gears_restfull_prefix}/${pojoDescriptor.name}/executeupload/${pojoid4upload}/${p.name}" enctype="multipart/form-data"  >
         ${p.name}
                    <input type="file" name="file" value="Upload File" >
                    <input type="submit" value="upload" />
                </form:form>
               <div id="theMeter_${p.name}" >
            <div id="progressBarText_${p.name}" class="progressBarText"></div>

            <div id="progressBarBox_${p.name}" class="progressBarBox">
                <div id="progressBarBoxContent_${p.name}" class="progressBarBoxContent"></div>
            </div>
        </div>
                <div id="output_upload_${p.name}"></div>
    </div>
    </c:if>
    </c:forEach>
    <br/>

    

